package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
@Slf4j
@RequiredArgsConstructor
public class UserController {
    @Autowired
    private final IUserService userService;

    /**
     * 保存用户
     * @param userFormDTO
     */
    @PostMapping
    @ApiOperation("保存用户")
    public void save(@RequestBody UserFormDTO userFormDTO){
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    /**
     *  删除用户
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public void delete(@PathVariable Long id){
        userService.removeById(id);
    }

    /**
     *  根据用户id查询用户
     * @param id
     */
    @GetMapping("/{id}")
    @ApiOperation("根据用户id查询用户")
    public UserVO getUser(@PathVariable Long id){
//        User user = userService.getById(id);
//        log.info("查询结果：{}", user);
//        return BeanUtil.copyProperties(user, UserVO.class);
        //根据用户id查询用户及收货地址
        return userService.queryUserAddressById(id);
    }

    /**
     * 根据id批量查询
     * @return
     */
    @GetMapping
    @ApiOperation("根据id批量查询")
    public List<UserVO> listUsers(@RequestParam("ids") List<Long> ids){
        List<User> list = userService.listByIds(ids);
        return BeanUtil.copyToList(list, UserVO.class);
    }

    /**
     * 根据id，金额扣减用户的余额
     * @param id
     * @param amount
     */
    @ApiOperation("根据id，金额扣减用户的余额")
    @PutMapping("/{id}/deduction/{amount}")
    public void updateBalanceById(@PathVariable("id") Long id,@PathVariable("amount") Integer amount){
        userService.deductBalanceById(id, amount);
    }


    @PostMapping("/list")
    @ApiOperation("根据查询条件userQuery 查询用户列表")
    public List<UserVO> queryList(@RequestBody UserQuery userQuery){
        String username = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer minBalance = userQuery.getMinBalance();
        Integer maxBalance = userQuery.getMaxBalance();
//        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda()
//                .like(StrUtil.isNotBlank(username), User::getUsername, username)
//                .eq(status != null, User::getStatus, status)
//                .ge(minBalance != null, User::getBalance, minBalance)
//                .le(maxBalance != null, User::getBalance, maxBalance);
//        List<User> userList = userService.list(queryWrapper);
        //改造为service中的lamdbaQuery
        List<User> userList = userService.lambdaQuery()
                .like(StrUtil.isNotBlank(username), User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
        return BeanUtil.copyToList(userList, UserVO.class);
    }
}
